package com.pipi.biyesheji.service.impl;

import com.pipi.biyesheji.dao.manager.CustomerManager;
import com.pipi.biyesheji.dao.model.CustomerDO;
import com.pipi.biyesheji.dao.query.CustomerQuery;
import com.pipi.biyesheji.dao.support.PageResult;
import com.pipi.biyesheji.pojo.Customer;
import com.pipi.biyesheji.service.CustomerService;
import com.pipi.biyesheji.util.BeanUtils;
import com.pipi.biyesheji.util.Page;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author pipi
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerManager customerManager;

    @Override
    public Customer foreLogin(Customer customer) {
        CustomerQuery query = new CustomerQuery();
        query.createCriteria().andNameEqualTo(customer.getName()).andPasswordEqualTo(customer.getPassword());
        List<CustomerDO> customerDOS = customerManager.selectByQuery(query);
        return CollectionUtils.isNotEmpty(customerDOS) ? buildCustomer(customerDOS.get(0)) : null;
    }

    @Override
    public void save(Customer customer) {
        CustomerDO customerDO = BeanUtils.createCloneBean(customer, CustomerDO.class);
        customerManager.insertSelective(customerDO);
    }

    @Override
    public Customer get(Long cstid) {
        CustomerDO customerDO = customerManager.selectByPrimaryKey(cstid);
        return buildCustomer(customerDO);
    }

    @Override
    public PageResult<Customer> list(Page page) {
        List<CustomerDO> customerDOS = customerManager.selectByQuery(null);
        return PageResult.createPageResult(buildCustomers(customerDOS));
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        CustomerDO customerDO = new CustomerDO();
        customerDO.setId(id);
        customerDO.setStatus(status);
        customerManager.updateByPrimaryKeySelective(customerDO);
    }

    @Override
    public Customer updatePassword(Customer customer) {
        CustomerDO customerDO = customerManager.selectByPrimaryKey(customer.getId());
        if (!customerDO.getPassword().equals(customer.getPassword())) {
            return null;
        }
        customerDO.setPassword(customer.getNewPassword());
        customerManager.updateByPrimaryKeySelective(customerDO);
        return buildCustomer(customerDO);
    }

    @Override
    public Customer queryByName(String name) {
        CustomerQuery query = new CustomerQuery();
        query.createCriteria().andNameEqualTo(name);
        List<CustomerDO> customerDOS = customerManager.selectByQuery(query);
        return CollectionUtils.isNotEmpty(customerDOS) ? buildCustomer(customerDOS.get(0)) : null;
    }

    @Override
    public void del(Long id) {
        customerManager.deleteByPrimaryKey(id);
    }

    private List<Customer> buildCustomers(List<CustomerDO> customerDOS) {
        return customerDOS.stream().map(this::buildCustomer).collect(Collectors.toList());
    }

    private Customer buildCustomer(CustomerDO customerDO) {
        if (Objects.isNull(customerDO)) {
            return null;
        }
        return BeanUtils.createCloneBean(customerDO, Customer.class);
    }

}
